<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.text.DateFormat"%>
<%@page import="edu.cmu.aes.project2.ws.billing.Invoice"%>
<%@page import="edu.cmu.aes.project2.BillingService"%>
<%@page import="java.util.Calendar"%>
<%@page import="edu.cmu.aes.project2.PlaceOrder"%>
<%@page import="org.bpel.sample.bpel.sample.PlaceorderRequestOrder"%>
<%@page import="edu.cmu.aes.project2.ws.data.UserInformationDto"%>
<%@page
	import="edu.cmu.aes.project2.ws.data.UserInformationServiceProxy"%>
<%@page import="edu.cmu.aes.project2.ws.data.UserInformationService"%>

<%
	request.setAttribute("title", "Invoice page");
	String username = UISecurityUtil.getUserName(request);
	if (username == null) {
		response.sendRedirect("unauthorizedAccess.jsp");
		return;
	}

	String errorMessage = "";
	boolean isError = false;
	UserInformationService userInfoService = new UserInformationServiceProxy();
	UserInformationDto userInfoDto = userInfoService
			.getUserInformation(username);
	Invoice[] invoices = null;
	String customerName = "";
	if (userInfoDto == null) {
		errorMessage = "User information is not found. Please login again or contact web master";
	} else {
		customerName = userInfoDto.getLastName()+", "+ userInfoDto.getFirstName();
		BillingService billingService = new BillingService();
		invoices = billingService.getInvoice(customerName);
		if(invoices == null){
			errorMessage = "Cannot connect to billing service";
			isError = true;
		}
	}
%>
<%@include file='/WEB-INF/template/header.jsp'%>
<%@include file='/WEB-INF/template/right-menu.jsp'%>
<div id="content">
	<br />
	<%
		if(isError || invoices == null){
			out.println("<font color='red'>"+ errorMessage+"</font><br/><br/>");
		}else{
			if(invoices.length == 0){
				out.println("<font color='red'>You don't have invoice information</font><br/><br/>");
			}else{
	%>
		Invoice information for <%=customerName%>
		<table id="detail-table" style="width: 400px">
			<tr>
				<th width=200>Amount</th>
				<th width=200>Paid date</th>
			</tr>
	<%
		SimpleDateFormat dateFormate = new SimpleDateFormat("MM/dd/yyyy");
		for(Invoice invoice:invoices){
	%>
		<tr>
			<td><%=invoice.getAmount() %></td>
			<td><%=dateFormate.format(invoice.getPaid_date().getTime()) %></td>
		</tr>
	<%		
		}
	%>
		</table>
	<%				
			}
		}
	%>
</div>
<%@include file='/WEB-INF/template/footer.jsp'%>
